package com.managertrade.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebMvcConfig  implements WebMvcConfigurer {
    @Autowired
    private JwtInterceptor jwtInterceptor;

    /**
     * 允许跨域访问
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*") // 使用这个兼容低版本
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

    /**
     * 注册拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**") // 拦截所有请求
                .excludePathPatterns(
                        "/login",          // 登录接口
                        "/register",       // 注册接口（如有）
                        "/favicon.ico",    // 图标
                        "/error",          // SpringBoot 默认错误路径
                        "/**/*.js", "/**/*.css", "/**/*.html", "/**/*.png", "/**/*.jpg", // 静态资源
                        "/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui/**" ,// Swagger（如使用）
                        "/api/ws/command"
                );
    }

    /**
     * 配置静态资源（可选）
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 如果你放了一些上传的静态文件在本地，可在此配置
        // registry.addResourceHandler("/upload/**").addResourceLocations("file:D:/upload/");
    }
}
